// ==UserScript==
// @name         深大慕课脚本
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  try to take over the world!
// @author       You
// @match        http://cce.org.uooconline.com/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=uooconline.com
// @grant        GM_log
// @grant        unsafeWindow
// @require      https://cdn.bootcdn.net/ajax/libs/lodash.js/4.17.19/lodash.js
// @require      https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.min.js
// ==/UserScript==

// window._lodash = _.noConflict();
(function() {
    'use strict';
    console.log('---匹配上了深大慕课---');
    let hideSider;
    const oldEventListenter = EventTarget.prototype.addEventListener;
    EventTarget.prototype.addEventListener = function(...args){
        const [type,e] = args;
        if(type === 'mouseout' && e.elem === $('html')[0])return;
        oldEventListenter.call(this,...args);
    }
    const loopFind = (condition,cb,delay=200,timeout=1000*30)=>{
        return new Promise((resolve,reject)=>{
            const start = Date.now();
            console.log('查找');
            if(condition()){
                console.log('找到了');
                resolve();
                return cb&&cb();
            }
            const timer = setInterval(()=>{
                if(condition()){
                    console.log('找到了');
                    cb&&cb();
                    resolve();
                    clearInterval(timer);
                }
                if(Date.now() - start > timeout){
                    reject();
                    clearInterval(timer);
                }
            },delay);
        });
    }
    const sel = document.querySelector.bind(document);
    const _ = window._;
    const $ = window.$;
    const dealVideo = async ()=>{
        const getVideoObj = ()=>$("#player_html5_api");
        const getVideo = ()=>getVideoObj()[0];
        await loopFind(getVideo);
        const videoObj = getVideoObj();
        const video = getVideo();
        videoObj.on('play', e=>{
            video.muted = true;
            video.playbackRate = 2;
        });
        const head = $(".learn-head");
        const right = $(".learn-main-right");
        const videoContent = $(".player-dimensions");
        let { offsetWidth, offsetHeight } = document.body;
        const headHeight = head.height();
        const rightWidth = right.width();
        hideSider = ()=>{
            head.hide();
            right.hide();
            videoContent.css({ height: offsetHeight, width: offsetWidth });
        };
        hideSider();
        const showSider = ()=>{
            head.show();
            right.show();
            videoContent.css({height: offsetHeight - headHeight,width: offsetWidth - rightWidth});
        };
        $(window).resize(()=>{
            offsetWidth = document.body.offsetWidth;
            offsetHeight = document.body.offsetHeight;
            hideSider();
        });
        const hideDebounce = _.debounce((e)=>{
            const headBottomY = head.offset().top + headHeight;
            const rightLeftX = right.offset().left;
            const { pageX, pageY } = e;
            if(pageX > rightLeftX || pageY < headHeight)return;
            hideSider();
        },600, { leading: false });
        $(document).on('mousemove',e=>{
            showSider();
            hideDebounce(e);
        });
        videoObj.click(e=>{
            video.play();
        });
        const outHideSider = _.debounce(()=>{
            video&&hideSider();
        },600, { leading: false });
        $('body').on('mouseleave',outHideSider);
        videoObj.on('pause',e=>{
            console.log("暂停了");
            setTimeout(()=>{
                if($(".vjs-mask")[0] || $(".layui-layer-shade")[0] || $(".layui-layer-page")[0]){
                    console.log("检测到有遮罩");
                    $(".vjs-mask").remove();
                    $(".layui-layer-shade").remove();
                    $(".layui-layer-page").remove();
                    video.play();
                    console.log("删除遮罩，开始播放");
                }
            },1500);
        });
    }
    dealVideo();
    window.onhashchange=function(event){
        dealVideo();
    }
    // Your code here...
})();